﻿Imports PBE.Market.Logic
Imports PBE.Market.Logic.Contracts
Imports System.Data
Imports System.Data.SqlClient


Namespace GUI
    Public Class frmTest

        Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
            Dim cnt As iContract

            Select Case txtType.Text
                Case "ANN"
                    cnt = New cContractANN(txtType.Text,
                                        "10",
                                        CDbl(txtBalance.Text),
                                        txtCurrency.Text,
                                        CDbl(txtInt.Text),
                                        CDate(ddMaturity.Value).Date,
                                        CDate(DReport.Value).Date,
                                        "100",
                                        "100")
                Case "RGM"
                    cnt = New cContractRGM(txtType.Text,
                                        "10",
                                        CDbl(txtBalance.Text),
                                        txtCurrency.Text,
                                        CDbl(txtInt.Text),
                                        CDate(ddMaturity.Value).Date,
                                        CDate(DReport.Value).Date,
                                        "100",
                                        "100")
                Case "PAM"
                    cnt = New cContractPAM(txtType.Text,
                                        "10",
                                        CDbl(txtBalance.Text),
                                        txtCurrency.Text,
                                        CDbl(txtInt.Text),
                                        CDate(ddMaturity.Value).Date,
                                        CDate(DReport.Value).Date,
                                        "100",
                                        "100")
                Case "CASH"
                    cnt = New cContractCash(txtType.Text,
                                        "10",
                                        CDbl(txtBalance.Text),
                                        txtCurrency.Text,
                                        CDbl(txtInt.Text),
                                        CDate(ddMaturity.Value).Date,
                                        CDate(DReport.Value).Date,
                                        "100",
                                        "100")
                Case "UMP"
                    cnt = New cContractUMP(txtType.Text,
                                        "10",
                                        CDbl(txtBalance.Text),
                                        txtCurrency.Text,
                                        CDbl(txtInt.Text),
                                        CDate(ddMaturity.Value).Date,
                                        CDate(DReport.Value).Date,
                                        "100",
                                        "100")

            End Select


            cnt.CalculatePaymnetSchedaul()
            txtResult.Clear()
            Dim strLine As String = "P# {0} , P Days {1}, P Date {2} , P Amnount {3} , PPMT {4} , IPMT {5}" & vbCrLf
            For Each ps As PaymentSchedual In cnt.PaymentSchedaul
                txtResult.Text &= String.Format(strLine, ps.intPaymentOrder, ps.intPaymentDays, ps.dtePaymentDate, ps.dblPaymentAmount, ps.dblPrincipalPayment, ps.dblInterestPayment)
            Next


            'Dim dteStart As Date = Now
            'Dim DBwrt As New Data.cDataResulrWriter
            'For intlooper As Integer = 0 To 999
            '    DBwrt.SaveDatatoDB(cnt)
            'Next
            'DBwrt.SaveData()

            'txtResult.Text &= "Dune" & vbCrLf
            'Dim dblTime As Double = DateDiff(Microsoft.VisualBasic.DateInterval.Second, Now, dteStart)
            'txtResult.Text &= dblTime.ToString & vbCrLf
        End Sub

        Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ddMaturity.Value = Now.Date.AddMonths(3)

        End Sub

        Private Sub btnDBTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDBTest.Click

            Dim intRasio, intCounter As Integer
            Dim DBwrt As Data.cDataResulrWriter

            Dim con As New SqlConnection(My.Settings.PBEConnectionString)
            Dim Cmd As New SqlCommand("Select top 100 * from Loans")
            Cmd.Connection = con

            con.Open()
            Dim DR As SqlDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
            DBwrt = New Data.cDataResulrWriter
            Debug.Print(Now.ToLongTimeString & "Start Proccessing")
            
            While DR.Read
                If DR(20).ToString.ToLower = "Retail Banking".ToLower Then
                    Dim cnt As New cContractANN("ANN",
                                                DR(5).ToString,
                                                CDbl(DR(10)),
                                                DR(0),
                                                CDbl(DR(14)) / 100,
                                                CDate(DR(13)),
                                                CDate(Now),
                                                DR(20),
                                                DR(21),
                                                DR(20) & "-" & DR(21))
                    cnt.CalculatePaymnetSchedaul()
                    DBwrt.SaveDatatoDB(cnt)

                Else
                    Select Case DR(21).ToString.ToLower
                        Case "Overdraft".ToLower
                            Dim cnt As New cContractUMP("UMP",
                                                        DR(5).ToString,
                                                        CDbl(DR(10)),
                                                        DR(0),
                                                        CDbl(DR(14)) / 100,
                                                        CDate(DR(13)),
                                                        CDate(Now),
                                                        DR(20),
                                                        DR(21),
                                                        DR(20) & "-" & DR(21))
                            cnt.CalculatePaymnetSchedaul()
                            DBwrt.SaveDatatoDB(cnt)

                        Case Else
                            Dim cnt As New cContractRGM("RGM",
                                                          DR(5).ToString,
                                                          CDbl(DR(10)),
                                                          DR(0),
                                                          CDbl(DR(14)) / 100,
                                                          CDate(DR(13)),
                                                          CDate(Now),
                                                          DR(20),
                                                          DR(21),
                                                          DR(20) & "-" & DR(21))
                            cnt.CalculatePaymnetSchedaul()
                            'DBwrt.SaveDatatoDB(cnt)
                            
                    End Select

                End If
                intCounter += 1
                prgProgressBar1.Value += 1
                Me.Text = String.Format("{0}/{1}", prgProgressBar1.Value, prgProgressBar1.Maximum)
                intRasio = CInt((CInt(prgProgressBar1.Value) / CInt(prgProgressBar1.Maximum)) * 100)
                prgProgressBar1.Refresh()
                lblRasio.Text = String.Format("{0}%", intRasio.ToString)
                lblRasio.Refresh()

                'Me.Refresh()
                If intCounter = 1000 Then
                    Debug.Print(String.Format(" Compltiopn resio : {0}% , Time {1}", intRasio.ToString, Now.ToLongTimeString))
                    intCounter = 0
                    DBwrt.SaveData()
                    DBwrt.Dispose()
                    DBwrt = New Data.cDataResulrWriter
                    Stop
                End If
            End While

            Debug.Print(Now.ToLongTimeString & " Proccessed and Saving Started")
            DBwrt.SaveData()
            Debug.Print(Now.ToLongTimeString & " Saved")
            DBwrt.Dispose()
            Debug.Print(Now.ToLongTimeString & " Sidposed")
        End Sub

        Private Sub btnDR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDR.Click

            Dim DBwrt As Data.cDataWriterDR

            Dim con As New SqlConnection(My.Settings.PBEConnectionString)
            Dim Cmd As New SqlCommand("Select * from Loans")
            Cmd.Connection = con
            txtResult.Text &= Now.ToLongTimeString & vbCrLf
            con.Open()
            Dim DR As SqlDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
            txtResult.Text &= Now.ToLongTimeString & vbCrLf
            DBwrt = New Data.cDataWriterDR
            Debug.Print(Now.ToLongTimeString)
            txtResult.Text &= Now.ToLongTimeString & vbCrLf

            While DR.Read
                If DR(20).ToString.ToLower = "Retail Banking".ToLower Then
                    Dim cnt As New cContractANN("ANN",
                                                DR(5).ToString,
                                                CDbl(DR(10)),
                                                DR(0),
                                                CDbl(DR(14)) / 100,
                                                CDate(DR(13)),
                                                CDate(DReport.Value),
                                                DR(20),
                                                DR(27),
                                                DR(28),
                                                DR(21))
                    cnt.CalculatePaymnetSchedaul()
                    DBwrt.SaveData(cnt)
                    cnt = Nothing
                Else
                    Select Case DR(21).ToString.ToLower
                        Case "Overdraft".ToLower
                            Dim cnt As New cContractUMP("UMP",
                                                        DR(5).ToString,
                                                        CDbl(DR(10)),
                                                        DR(0),
                                                        CDbl(DR(14)) / 100,
                                                        CDate(DR(13)),
                                                        CDate(Now),
                                                        DR(20),
                                                        DR(21),
                                                        DR(20) & "-" & DR(21))
                            cnt.CalculatePaymnetSchedaul()
                            DBwrt.SaveData(cnt)
                            cnt = Nothing
                        Case Else
                            Dim cnt As New cContractRGM("RGM",
                                                            DR(5).ToString,
                                                            CDbl(DR(10)),
                                                            DR(0),
                                                            CDbl(DR(14)) / 100,
                                                            CDate(DR(13)),
                                                            CDate(DReport.Value),
                                                            DR(20),
                                                            DR(27),
                                                            DR(28),
                                                            DR(21))
                            cnt.CalculatePaymnetSchedaul()
                            DBwrt.SaveData(cnt)
                            cnt = Nothing
                    End Select

                    GC.Collect()

                End If
            End While
            txtResult.Text &= Now.ToLongTimeString & vbCrLf
            DBwrt.CloseConnection()
            txtResult.Text &= Now.ToLongTimeString & vbCrLf
        End Sub
    End Class
End Namespace